package com.flexcloudsdk.web;

import java.io.IOException;

import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flexcloudsdk.apiidentity.CloudPublicCertificate;

public class CloudAPIIdentityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger
			.getLogger(CloudAPIIdentityServlet.class.getName());
	@SuppressWarnings("unused")
	private CloudPublicCertificate cert = new CloudPublicCertificate();
	private String sourceClass;
	private String sourceMethod;
	// constants for method calls
	// these wil be switched to uint when its released in production
	// keep them as text for debugging
	public static final String createCertificate = "createCertificate";
	public static final String getCertificateName = "getCertificateName";
	public static final String getX509CertificateInPermFormat = "getX509CertificateInPermFormat";

	public CloudAPIIdentityServlet() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudAPIIdentityServlet.class.getName();
		log.info("*****CloudAPIIdentityServlet constructor********");
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	}

	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		resp.setContentType("text/xml");
		PrintWriter writer = resp.getWriter();
		writer.println("<?xml version=\"1.0\"?>");
		writer.println("<root>");

		String operation = req.getParameter("operation");
		if (operation == null) {
			handleEmptyParameters(writer, req);
		} else if (operation.isEmpty()) {
			handleEmptyOperation(writer, req);
		} else if (operation.equalsIgnoreCase(createCertificate)) {
			createCertificate(writer, req);
		} else if (operation.equalsIgnoreCase(getCertificateName)) {
			getCertificateName(writer, req);
		}
		
		writer.println("</root>");
		log.exiting(sourceClass, sourceMethod);
	}

	private void handleEmptyParameters(PrintWriter writer,
			HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		writer.println("<result>No operation found while calling "
				+ req.getServerName() + "servlet</result>");
	}

	private void handleEmptyOperation(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		writer.println("<result>Must pass an operation in calling "
				+ req.getServerName() + "servlet</result>");
	}

	private void getCertificateName(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

	}

	private void createCertificate(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

	}


}
